{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "55a088ce",
   "metadata": {},
   "source": [
    "### list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4433d32e",
   "metadata": {},
   "source": [
    "list时一种有序的集合，可以随时添加和删除其中的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a2db3d8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates = ['Michael', 'Bob', 'Tracy']\n",
    "classmates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "af3c3079",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(classmates) #获取list元素的个数"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a19a8e62",
   "metadata": {},
   "source": [
    "用索引来访问list中每一个位置的元素，和数组的用法基本一致。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e9a20cb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Michael'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d0bf046a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bob'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "311a5745",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Tracy'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75e839fe",
   "metadata": {},
   "source": [
    "list是一个可变有序表，所以可以往list中追加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d00492af",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy', 'Adam']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.append('Adam')\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "073b75d5",
   "metadata": {},
   "source": [
    "也可以把元素插入到指定的位置，比如索引号为1的位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f937e3c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.insert(1,'Jack')\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1e2c8ffd",
   "metadata": {},
   "source": [
    "删除末尾元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4f02e572",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Jack', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.pop()\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "595085fa",
   "metadata": {},
   "source": [
    "删除指定位置的元素，用pop(i)，i为索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f5609aa8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Bob', 'Tracy']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates.pop(1)\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc5e3286",
   "metadata": {},
   "source": [
    "把某个元素替换成别的元素，可以直接赋值给对应的索引位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "545091fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Michael', 'Sarah', 'Tracy']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "classmates[1] = 'Sarah'\n",
    "classmates"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc247cbe",
   "metadata": {},
   "source": [
    "list元素的数据类型也可以不同"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e2312155",
   "metadata": {},
   "outputs": [],
   "source": [
    "L = ['Apple', 123, True]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ebce57af",
   "metadata": {},
   "source": [
    "list元素也可以时另一个list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "095b0436",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = ['python', 'java', ['asp','php'], 'scheme']\n",
    "len(s)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6605a23",
   "metadata": {},
   "source": [
    "### tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df3ac5a9",
   "metadata": {},
   "source": [
    "元组初始化后不能再修改，因此它没有append，insert的方法，其他获取方法一样，但是不能赋值成另外的元素。\n",
    "tuple的不可变，可以使代码更加安全。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "576c6179",
   "metadata": {},
   "source": [
    "当tuple著有一个元素定义时，必须加一个逗号，来消除歧义，否则会被误认为是带一个元素的整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "884785cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1)\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c2746a6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1,)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (1,)\n",
    "t"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c4cd4e",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b2b7abf",
   "metadata": {},
   "source": [
    "list和tuple都是Python内置的有序集合，一个可变，一个不可变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ef56157",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
